/**
 * 
 */
package zaphod.project1;

import java.io.InputStream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;

import zaphod.project1.editparts.Project1Editor;

final class XMLLoadingJob extends Job
{
    XMLLoadingJob( String name )
    {
        super( name );
    }

    @Override
    protected IStatus run( IProgressMonitor monitor )
    {
        IWorkspace workspace = ResourcesPlugin.getWorkspace();
        IWorkspaceRoot root = workspace.getRoot();
        IProject project;
        final IFile file;
        InputStream source;
        boolean force = true;
        boolean keepHistory = true;

        project = root.getProject( "test" );
        try
        {

            if ( !project.exists() )
            {
                project.create( monitor );
            }

            if ( !project.isOpen() )
            {
                project.open( monitor );
            }

            file = project.getFile( "example.xml" );
            source = this.getClass().getClassLoader().getResourceAsStream(
                "zaphod/project1/example.xml" );

            if ( !file.exists() )
            {
                file.create( source, force, monitor );
            }
            else
            {
                file.setContents( source, force, keepHistory, monitor );
            }
            final Exception except[] = new Exception[]
            { null };

            // open the editor;
            Display.getDefault().syncExec( new Runnable()
            {

                public void run()
                {
                    IWorkbench workbench;
                    IWorkbenchWindow window;
                    IWorkbenchPage page;
                    workbench = PlatformUI.getWorkbench();
                    window = workbench.getActiveWorkbenchWindow();
                    page = window.getActivePage();

                    IEditorInput input = new FileEditorInput( file );
                    try
                    {
                        page.openEditor( input, Project1Editor.ID );
                    }
                    catch ( PartInitException e )
                    {
                        e.printStackTrace();
                        except[0] = e;
                    }

                }
            } );

            // if failed to open the editor,
            if ( except[0] != null )
            {
                return Status.CANCEL_STATUS;
            }

        }
        catch ( CoreException e )
        {
            e.printStackTrace();
            return Status.CANCEL_STATUS;
        }

        return Status.OK_STATUS;
    }
}